package proffesorintro;

import java.util.List;

import com.andro.chansung.ConstantName;
import com.andro.chansung.DB;
import com.andro.chansung.R;

import departmentintro.Proffesor;

import android.app.Activity;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

public class DepartmentProffesorIntroFragment extends Fragment {
	
	private Activity activity;
	private ProffesorHandler handler;
	private ListView listView;
	
	private ProgressDialog pd;
	
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		activity = getActivity();
		handler = new ProffesorHandler();
		
		SQLiteDatabase db = activity.openOrCreateDatabase(ConstantName.DATABASE_NAME, Context.MODE_WORLD_WRITEABLE, null);
		DB.removeDepartmentProffesorTable(db);
		DB.createNoticeTable(db);
		db.close();
		
		View view = inflater.inflate(R.layout.department_proffesor_intro_layout, container, false);
		
		listView = (ListView)view.findViewById(R.id.department_proffesor_list_view);
		
		DeparmentProffesorUpdateThread thread = new DeparmentProffesorUpdateThread(getActivity(), handler);
		pd = ProgressDialog.show(getActivity(), "로딩중", "교수 소개 페이지 로딩중...");
		thread.start();
		
		return view;
	}
	
	public class ProffesorHandler extends Handler {
		public void handleMessage(Message msg) {
			SQLiteDatabase db = activity.openOrCreateDatabase(ConstantName.DATABASE_NAME, Context.MODE_WORLD_READABLE, null);
			List<Proffesor> proffesors = DB.getDataFromProffesorTable(db);
			db.close();
			
			ProffesorAdapter adapter = new ProffesorAdapter(activity, proffesors);
			listView.setAdapter(adapter);
			
			pd.dismiss();
		}
	}
}
